package com.sundablog.controller.backend.index;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sundablog.pojo.AdminUser;
import com.sundablog.result.BaseResult;
import com.sundablog.result.MenuResult;
import com.sundablog.service.backend.index.IndexService;
import com.sundablog.service.backend.system.upms.filesystem.FilesyStemService;
import com.sundablog.utlis.CaptchaUtil;
import com.sundablog.utlis.RedisUtil;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;

/**
 * 首页登入控制器
 * @ClassName:  IndexController   
 * @Description:首页登入控制器   
 * @author: 哒哒 
 * @date:   2018年3月18日 下午12:45:03   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Controller
public class IndexController {
	
	@Autowired
	private FilesyStemService filesyStemService;
	
	@Autowired
	private IndexService indexService;
	
	/**
	 * 后台界面显示
	 * @Title: index   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequestMapping("/index")
	public String index() {
		return "/index/index";
	}
	
	/**
	 * 后台界面显示
	 * @Title: main   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequestMapping("/main")
	public String main() {
		return "/index/main";
	}
	
	/**
	 * 获取用户信息
	 * @Title: getUser   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequestMapping("/getUser")
	@ResponseBody
	public BaseResult getUser() {
		AdminUser user = (AdminUser) SecurityUtils.getSubject().getPrincipal();
		String avatar = user.getAvatar();
		String url = filesyStemService.getDownloadUrl(avatar);
		user.setAvatar(url);
		
		return BaseResult.build(200, "", user);
	}
	
	/**
	 * 解锁
	 * @Title: unlockSys   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param pass
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequestMapping("/unlockSys")
	@ResponseBody
	public BaseResult unlockSys(String pass) {
		AdminUser user = (AdminUser) SecurityUtils.getSubject().getPrincipal();
		String avatar = user.getAvatar();
		String string = DigestUtil.md5Hex(pass + user.getSalt());
		if (user.getPassword().equals(string)) {
			return BaseResult.ok();
		} else {
			return BaseResult.build(209, "密码错误");
		}
		
		
		
	}
	
	/**
	 * 获取后台菜单
	 * @Title: getMenuResult   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @return      
	 * @return: Map<Object,Object>      
	 * @throws
	 */
	@RequestMapping("/getMenuResult")
	@ResponseBody
	public Map<Object, Object> getMenuResult() {
		AdminUser user = (AdminUser) SecurityUtils.getSubject().getPrincipal();
		String backendMenu = RedisUtil.get("backend_Menu_"+user.getUserId());
		if (StrUtil.isEmpty(backendMenu)) {
			List<MenuResult> list = indexService.getMenuResult(user.getUserId());
			Map<Object, Object> map = new HashMap<>(4);
			map.put("code", "0");
			map.put("msg", "success");
			map.put("data", list);
			String jsonStr = JSONUtil.toJsonStr(list);
			RedisUtil.set("backend_Menu_"+user.getUserId(), jsonStr,RedisUtil.EXRP_HOUR);
			return map;
		} else {
			Map<Object, Object> map = new HashMap<>(4);
			map.put("code", "0");
			map.put("msg", "success");
			JSONArray jsonArray = JSONUtil.parseArray(backendMenu);
			List<MenuResult> list = jsonArray.toList(MenuResult.class);
			map.put("data", list);
			return map;
		}
	}
	

	
	
}
